<?php

namespace app\index\model;

class Ajax
{
  public $db;

  function __construct()
  {
    $this->db = $GLOBALS['db'];
  }

  function feedback_post()
  {
    $arr['f_name'] = !empty($_POST['name']) ? str_safe($_POST['name']) : '';
    $arr['f_content'] = !empty($_POST['message']) ? str_safe($_POST['message']) : '';
    foreach ($arr as $val) {
      if ($val == '') alert_back('请核对信息后重试!');
    }
    $arr['f_tel'] = !empty($_POST['tel']) ? str_safe($_POST['tel']) : '';
    $arr['f_title'] = !empty($_POST['subject']) ? str_safe($_POST['subject']) : '';
    $arr['f_email'] = !empty($_POST['email']) ? str_safe($_POST['email']) : '';
    $arr['f_date'] = gmtime();

    if ($GLOBALS['db']->autoExecute("feedback", $arr, 'INSERT')) {
      alert_back('您的信息已提交, 谢谢您对我们的支持!');
    }
    alert_back('请核对信息后重试!');
  }

  function feedback_post_mail()
  {
    $arr['f_name'] = !empty($_POST['name']) ? str_safe($_POST['name']) : '';
    $arr['f_content'] = !empty($_POST['message']) ? str_safe($_POST['message']) : '';
    foreach ($arr as $val) {
      if ($val == '') alert_back('请核对信息后重试!');
    }
    $arr['f_tel'] = !empty($_POST['tel']) ? str_safe($_POST['tel']) : '';
    $arr['f_title'] = !empty($_POST['subject']) ? str_safe($_POST['subject']) : '';
    $arr['f_email'] = !empty($_POST['email']) ? str_safe($_POST['email']) : '';

    $mail_subject = '[' . $GLOBALS['cms']['name'] . '] 留言邮件';
    $mail_body = '姓名:' . $arr['f_name'] . '<br>';
    $mail_body .= '电话:' . $arr['f_tel'] . '<br>';
    $mail_body .= '电邮:' . $arr['f_email'] . '<br>';
    $mail_body .= '主题:' . $arr['f_title'] . '<br>';
    $mail_body .= '内容:' . $arr['f_content'] . '<br>';

    if (smtp_mail(SMTP_RECIEVER, $mail_subject, $mail_body)) {
      alert_back('您的信息已提交,谢谢您对我们的支持!');
    }
    alert_back('发送失败请稍后重试!');
  }

  function subscribe()
  {
    $sub_mail = str_safe($_POST['sub_mail']);
    if (check($sub_mail, '', 'email')) {
      $arr['sub_name'] = !empty($_POST['sub_name']) ? str_safe($_POST['sub_name']) : '';
      $arr['sub_sex'] = !empty($_POST['sub_sex']) ? str_safe($_POST['sub_sex']) : '';
      $arr['sub_mail'] = $sub_mail;
      $arr['sub_addr'] = !empty($_POST['sub_addr']) ? str_safe($_POST['sub_addr']) : '';
      $arr['sub_post'] = !empty($_POST['sub_post']) ? str_safe($_POST['sub_post']) : '';
      $arr['sub_date'] = gmtime();
      if ($GLOBALS['db']->getRow("SELECT * FROM subscribe WHERE sub_mail = '$sub_mail'")) {
        alert_back($GLOBALS['lang']['reg']['email_existing']);
      }
      $GLOBALS['db']->autoExecute("subscribe", $arr, 'INSERT');
      alert_href('您已加入邮件订阅!', './');
    }
    alert_back($GLOBALS['lang']['reg']['email_error']);
  }

  function mail()
  {
    $size = 10000;
    $count = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM subscribe");
    if ($count) {
      if (mk_dir(SUBSCRIBE_DIR) === false) {
        alert_back($GLOBALS['lang']['mkdir_error']);
      }
      for ($i = 0; $i < $count; $i += $size) {
        $res = $GLOBALS['db']->getAll("SELECT * FROM subscribe ORDER BY id ASC LIMIT $i, $size");
        foreach ($res as $key => $value) {
          $str = '';
          if ($key == 0) {
            $str = $value['sub_name'] . "," . $value['sub_sex'] . "," . $value['sub_mail'] . "," . $value['sub_addr'] . "," . $value['sub_post'] . "," . local_date('Y-m-d', $value['sub_date']);
          } else {
            $str .= PHP_EOL . $value['sub_name'] . "," . $value['sub_sex'] . "," . $value['sub_mail'] . "," . $value['sub_addr'] . "," . $value['sub_post'] . "," . local_date('Y-m-d', $value['sub_date']);
          }
        }
        $fp = fopen("subscribe_$i.txt", "w+");
        fwrite($fp, $str);
        fclose($fp);
      }
      alert_back($GLOBALS['lang']['msg_success']);
    }
    alert_back($GLOBALS['lang']['msg_failed']);
  }

  function cart()
  {
    if (isset($_POST['list']) && isset($_POST['num']) && isset($_POST['amount'])) {
      $arr = array();
      $arr['list'] = json_decode(str_replace("\\", "", $_POST['list']), true);
      $arr['num'] = $_POST['num'];
      $arr['amount'] = $_POST['amount'];
      $_SESSION['cart'] = $arr;

      $res['err'] = 'n';
      $res['url'] = 'cart.php';
    } else {
      $res["err"] = 'y';
      $res["url"] = '';
    }
    die(json_encode($res, JSON_NUMERIC_CHECK));
  }

  function __destruct()
  {
  }
}